<?php
/*
 * 字符串处理类
 * @author andery <andery@foxmail.com>
 * @link http://www.minophp.com/
 * @version $Id: CDateTime.lib.php 2010-07-08 23:12:10Z andery $
 */
class CString{
	/**
	 * 字符串截取
	 * 中文字符串和符号当一个长度处理
	 * @param string $str
	 * @param int $len
	 * @param string $dot
	 * @return string
	 */
	public static function cutstr($str, $len, $dot='...')
	{
	    $i = $ti = 0;
	    if (strlen($str) < $len) return $str;
	    $charset = self::is_utf8($str);
	    if ($charset) {
	        while ($ti < $len) {
	        	if(!isset($str[$i])){
	        		break;
	        	}
	            $t = ord($str[$i]);
	            if ($t >= 224) {
	                $i += 3;
	            } elseif ($t >= 192) {
	        	    $i += 2;
	            } else {
	                $i++;
	            }
	            if ($i>$len+1) break;
	            $ti++;
	        }
	    } else {
	        for($ti = 0; $ti < $len; $ti++) {
	            $t = ord($str[$i]);
	            if ($t > 127) {
	                $i += 2;
	            } else {
	                $i++;
	            }
	        }
	    }
	    $rt = substr($str, 0, $i);
	    return $rt;
	}
	
    /**
     * 对字符串和数组遍历转义
     * @param mixed
     * @return mixed
     */
    public static function saddslashes($value)
    {
        if (empty($value)) {
            return $value;
        } else {
            return is_array($value) ? array_map(array('CString','saddslashes'), $value) : addslashes($value);
        }
    }
    
    /**
     * HTML标签转换
     * @param mixed
     * @return mixed
     */
    public static function encode($data)
    {
        if(is_array($data)) {
            $d=array();
            foreach($data as $key=>$value) {
                if(is_string($key))
                    $key=htmlspecialchars($key,ENT_QUOTES,CConf::get('system.charset'));
                if(is_string($value))
                    $value=htmlspecialchars($value,ENT_QUOTES,CConf::get('system.charset'));
                else if(is_array($value))
                    $value=self::encode($value);
                $d[$key]=$value;
            }
            return $d;
        } elseif(is_string($data)) {
            return htmlspecialchars($data,ENT_QUOTES,CConf::get('system.charset'));
        } else {
            throw new CException(CLang('encode_data_type_invalid'));
        }
    }
    
    /**
     * 判断字符串编码
     */
    public static function is_utf8($string) {
        return preg_match('%^(?:
            [\x09\x0A\x0D\x20-\x7E]            # ASCII
            | [\xC2-\xDF][\x80-\xBF]            # non-overlong 2-byte
            |  \xE0[\xA0-\xBF][\x80-\xBF]        # excluding overlongs
            | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}  # straight 3-byte
            |  \xED[\x80-\x9F][\x80-\xBF]        # excluding surrogates
            |  \xF0[\x90-\xBF][\x80-\xBF]{2}    # planes 1-3
            | [\xF1-\xF3][\x80-\xBF]{3}          # planes 4-15
            |  \xF4[\x80-\x8F][\x80-\xBF]{2}    # plane 16
       )*$%xs', $string);
    }
    
    /**
     * 判断是否邮箱
     */
    public static function is_email($email)
    {
        $chars = "/^([a-z0-9+_]|\\-|\\.)+@(([a-z0-9_]|\\-)+\\.)+[a-z]{2,5}\$/i";
        if (strpos($email, '@') !== false && strpos($email, '.') !== false) {
            if (preg_match($chars, $email)) {
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }
}